using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.Web;
using CMS.Presenters.Views;
using CMS.Presenters.Presenters;
using CMS.Core.Domain;
using System.Collections.Generic;
using CMS.Data;

namespace CMS.Web.ListManager
{
    public partial class ListLichTiep : BaseUserControl, IListLichTiepView
    {        
        ListLichTiepPresenter presenter;
        IList<LichTiep> objIList;
        bool isRole = false;
        NguoiSuDungInfo userInfo;

        public ListLichTiep()
        {
            this.presenter = new ListLichTiepPresenter(this, NHibernateDaoFactory.Instance.GetLichTiepDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            userInfo = (NguoiSuDungInfo)Session["UserInfo"];
            presenter.LoadViewFromModel(userInfo.ID);
            if (!this.IsPostBack)
            {
                this.gridObj.DataSource = this.objIList;
                this.gridObj.DataBind();
            }

            if (this.IsHuyCongTT())
            {
                this.lbnXoa.Visible = true;
            }
            else
            {
                this.lbnXoa.Visible = false;
            }
            
            this.lbnXoa.Attributes.Add("onclick", "return doSetStatus(2)");
            this.lbnHienThi.Attributes.Add("onclick", "return doSetStatus(1)");
        }

        protected void gridObj_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gridObj.DataSource = this.objIList;
            gridObj.PageIndex = e.NewPageIndex;
            gridObj.DataBind();
        }        

        protected void gridObj_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                // Disabled control 
                foreach (GridViewRow row in gridObj.Rows)
                {
                    VanBan obj = VanBanDao.Instance.GetById(Convert.ToInt32(gridObj.DataKeys[row.RowIndex].Value.ToString()));
                    
                }

                foreach (Control c in e.Row.Cells[0].Controls)
                {
                    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.PagerTable"))
                    {
                        Table tbl = (Table)c;
                        TableCell cell = new TableCell();
                        cell.Text = "Trang ";
                        cell.HorizontalAlign = HorizontalAlign.Right;
                        tbl.Rows[0].Cells.AddAt(0, cell);
                    }
                }
            }
        }

        // For Delete button   
        protected void lbnXoa_Click(object sender, EventArgs e)
        {
            LichTiepDao iObjDAO;
            foreach (GridViewRow row in gridObj.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chkSelect");

                if (cb.Checked)
                {
                    int code = Convert.ToInt32(gridObj.DataKeys[row.RowIndex].Value);
                    // Delete file ref
                    iObjDAO = new LichTiepDao();
                    LichTiep obj = iObjDAO.GetById(code);

                    if (obj != null)
                    {
                        if (!obj.TenFileDinhKem.Equals(""))
                        {
                            Files.Instance.DeleteFile(true, "lichtiep", obj.TenFileDinhKem);
                        }
                        presenter.ObjToDelete(code);
                    }                  
                }
            }

            presenter.LoadViewFromModel(userInfo.ID);
            this.gridObj.DataSource = this.LichTiepList;
            gridObj.DataBind();
        }

        // For State
        protected void lbnHienThi_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in gridObj.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chkSelect");

                if (cb.Checked)
                {
                    int code = Convert.ToInt32(gridObj.DataKeys[row.RowIndex].Value);
                    presenter.ObjToUpdate(code);
                }
            }
            presenter.LoadViewFromModel(userInfo.ID);
            this.gridObj.DataSource = this.LichTiepList;
            gridObj.DataBind();
        }

        protected void lbnThem_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=ADD");
        }       

        public string ShowFileDinhKem(string fileName)
        {
            return (!fileName.Equals("")) ? "" : "none";
        }


        #region IListLichTiepView Members

        public System.Collections.Generic.IList<LichTiep> LichTiepList
        {
            get { return this.objIList; }
            set { this.objIList = value; }
        }

        public bool IsRole
        {
            get
            {
                return this.isRole;
            }
            set
            {
                this.isRole = value;
                this.lbnThem.Visible = value;
                this.lbnXoa.Visible = value;
            }
        }

        public bool IsValid
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        #endregion        
    }
}